package com.qf.j2112.controller;

import com.github.pagehelper.PageInfo;
import com.qf.j2112.po.SysContact;
import com.qf.j2112.po.SysOrder;
import com.qf.j2112.po.SysYiZhan;
import com.qf.j2112.service.RabbitService;
import com.qf.j2112.service.SysContactService;
import com.qf.j2112.service.SysOrderService;
import com.qf.j2112.service.SysYiZhanService;
import com.qf.j2112.utils.JwtUtil;
import com.qf.j2112.vo.AjaxResult;
import com.qf.j2112.vo.Cartvo;
import com.qf.j2112.vo.Chargevo;
import com.qf.j2112.vo.Contactvo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 订单Controller
 * @author j2112
 * @date 2022-04-12
 */

@Slf4j
@CrossOrigin
@Controller
public class SysOrderController {

    @Autowired
    private SysOrderService sysOrderService;

    @Autowired
    private SysYiZhanService sysYiZhanService;

    @Autowired
    private SysContactService sysContactService;

    @Autowired
    private RabbitService rabbitService;

    /*查询驿站列表*/
    @ResponseBody
    @RequestMapping(value = "yizhanlist",method = RequestMethod.GET)
    public AjaxResult listYizhan(){
        List<SysYiZhan> list=sysYiZhanService.selectSysYiZhanList();
        if(!ObjectUtils.isEmpty(list)){
            return AjaxResult.success("查询成功",list);
        }else {
            return AjaxResult.error("查询失败",null);
        }
    }

    /*查询用户的联系列表*/
    @ResponseBody
    @RequestMapping(value = "contactlist",method = RequestMethod.GET)
    public AjaxResult listContact(HttpServletRequest request){
        String token=request.getHeader("token");
        token= JwtUtil.getUsrInfoFromToken(token);
        Integer userId=Integer.valueOf(token);
        log.info("token被解析"+userId);
        List<SysContact> list=sysContactService.selectSysContactListByUserId(userId);

        if(!ObjectUtils.isEmpty(list)){
            Set<Contactvo> contactvos=new HashSet<>();
            for (SysContact con:list){
                Contactvo vo=new Contactvo(
                        con.getContactId(),
                        con.getContactName(),
                        con.getContactTel(),
                        con.getIsDefault()
                );
                contactvos.add(vo);
            }
            return AjaxResult.success("查询成功",contactvos);
        }else {
            return AjaxResult.error("查询失败",null);
        }
    }

    /*查询用户的订单列表*/
    @ResponseBody
    @RequestMapping(value = "/myorder",method = RequestMethod.GET)
    public AjaxResult listMyOrder(HttpServletRequest request) {
        String token=request.getHeader("token");
        token= JwtUtil.getUsrInfoFromToken(token);
        Integer userId=Integer.valueOf(token);
        log.info("token被解析"+userId);
        List<SysOrder> list = sysOrderService.selectSysOrderListByUserId(userId);
        return AjaxResult.success("查询成功",list);
    }

    /*查询订单列表*/
    @ResponseBody
    @RequestMapping(value = "/orderlist",method = RequestMethod.GET)
    public AjaxResult listorder1() {
        List<SysOrder> list = sysOrderService.selectSysOrderList();
        return AjaxResult.success(list);
    }

    /*查询订单列表2*/
    @ResponseBody
    @RequestMapping(value = "/orderlist2",method = RequestMethod.GET)
    public List<SysOrder> listorder2(@RequestParam(value = "size") Integer size,
                                     @RequestParam(value = "page") Integer page,
                                     @RequestParam(value = "status1") Integer status1,
                                     @RequestParam(value = "status2") Integer status2) {
         List<SysOrder>  list= sysOrderService.selectOrder(size,page,status1,status2);
         return list;
    }

    /*查询订单列表3*/
    @ResponseBody
    @RequestMapping(value = "/orderlist3",method = RequestMethod.GET)
    public PageInfo listorder3(@RequestParam(value = "size") Integer size,
                               @RequestParam(value = "page") Integer page,
                               @RequestParam(value = "status1") Integer status1,
                               @RequestParam(value = "status2") Integer status2) {
        PageInfo pageInfo = sysOrderService.selectOrderNum2(size,page,status1,status2);
        return pageInfo;
    }

    /*获取订单详细信  */
    @ResponseBody
    @RequestMapping(value = "/select/{orderId}",method = RequestMethod.GET)
    public AjaxResult getInfo(@PathVariable("orderId") Integer orderId) {
        return AjaxResult.success("查询成功",sysOrderService.selectSysOrderByOrderId(orderId));
    }

    /*新增订单 */
    @ResponseBody
    @RequestMapping(value = "/orderadd",method = RequestMethod.POST)
    public AjaxResult add(@RequestBody Cartvo cartvo, HttpServletRequest request) {
        String token=request.getHeader("token");
        token= JwtUtil.getUsrInfoFromToken(token);
        Integer userId=Integer.valueOf(token);
        log.info("token被解析"+userId);
        cartvo.setUserId(userId);
        log.info("接收到定单请求"+cartvo);
        SysOrder sysOrder=sysOrderService.insertSysOrder(cartvo);
        if(sysOrder.getOrderId()!=null){
            return AjaxResult.success("添加成功",sysOrder.getOrderId());
        }else
        return AjaxResult.error("添加失败");
    }

    /*修改订单*/
    @RequestMapping(value = "orderupd",method = RequestMethod.PUT)
    public AjaxResult edit(@RequestBody SysOrder sysOrder) {
        return AjaxResult.success("修改成功",sysOrderService.updateSysOrder(sysOrder));
    }

    /*修改订单状态为已支付*/
    @ResponseBody
    @RequestMapping(value = "paysuccess",method = RequestMethod.PUT)
    public AjaxResult paysuccess( Integer orderId) {
        SysOrder sysOrder=new SysOrder();
        sysOrder.setOrderId(orderId);
        //1-->支付成功状态
        sysOrder.setOrderOrderstatusId(1);
        log.info("订单为:"+sysOrder);
        Boolean flag=sysOrderService.updateSysOrder(sysOrder);
        if(flag){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    /*删除订单*/
    @ResponseBody
	@DeleteMapping("/orderdel/{orderIds}")
    public AjaxResult remove(@PathVariable Integer[] orderIds) {
        return AjaxResult.success("删除成功",sysOrderService.deleteSysOrderByOrderIds(orderIds));
    }

    /* 修改订单状态*/
    @ResponseBody
    @RequestMapping(value = "updateStatus",method = RequestMethod.PUT)
    public AjaxResult updateOrder( @RequestParam(value = "orderId") Integer orderId,
                                   @RequestParam(value = "statusId") Integer statusId) {
        SysOrder sysOrder=new SysOrder();
        sysOrder.setOrderId(orderId);
        sysOrder.setOrderOrderstatusId(statusId);
        log.info("订单为:"+sysOrder);
        Boolean flag=sysOrderService.updateSysOrder(sysOrder);
        if(flag){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    /*用户用余额兑换商品*/
    @ResponseBody
    @RequestMapping(value = "goBuy",method = RequestMethod.POST)
    public AjaxResult buyGoods(Integer orderId,HttpServletRequest request){
        SysOrder sysOrder=sysOrderService.selectSysOrderByOrderId(orderId);
        rabbitService.sendReduceRequest(sysOrder);
        return AjaxResult.success();
    }

    /*用户用余额充值*/
    @ResponseBody
    @RequestMapping(value = "charge",method = RequestMethod.POST)
    public AjaxResult charge(String money,HttpServletRequest request){
        String token=request.getHeader("token");
        token= JwtUtil.getUsrInfoFromToken(token);
        Integer userId=Integer.valueOf(token);
        log.info("token被解析"+userId);
        Integer money1=Integer.valueOf(money);
        Chargevo chargevo=new Chargevo(userId,money1);
        rabbitService.sendChargeRequest(chargevo);
        return AjaxResult.success();
    }
}
